/**
 * 初始化 首页
 */

layui.define(['base','contabs'], function (exports) {
    var $=layui.jquery,layer=layui.layer,$setting=layui.config,$http=layui.http,$global=layui.common,contabs = layui.contabs;
    var request = $http.get($setting.config.user.info);
    request.async = false;

   var $editPassModal=$('#editPassModal'),$newpassword=$('#newpassword'),$oldpassword=$('#oldpassword'),$newpassAgain=$("#newpassAgain");
    
    var dta = $global.fn.getUserData();
    var loadGlobalMenu =  function () {
        $('#index').attr('data-id', dta.defaultIndexUrl);
        var targetUrl = contabs.assembleUrl(dta.defaultIndexUrl);
        $('#iframe').attr('data-id', dta.defaultIndexUrl).attr('src', targetUrl);

        var menuAry = new Array();
        $(dta.menuTree).each(function (index, item) {
            var menu = '';    //拼接菜单tree
            if (item.children.length > 0) {  //二级菜单
                menu = '<li>' +
                    '<a href="#">' +
                    '<i class="' + item.icon + '"></i>' +
                    '<span class="nav-label">' + item.text + '</span>' +
                    '<span class="fa arrow"></span>' +
                    '</a><ul class="nav nav-second-level">';
                var childAry = new Array();
                $(item.children).each(function (index, item) {
                    childAry.push('<li>' +
                        '<a class="J_menuItem" href="' + item.url + '">' +
                        '<i class="' + item.icon + '"></i>' +
                        '<span class="nav-label">' + item.text + '</span>' +
                        '</a>' +
                        '</li>');
                });
                menu += childAry.join('') + '</ul></li>';
            } else {        //一级菜单
                menu = '<li>' +
                    '<a class="J_menuItem" href="' + item.url + '">' +
                    '<i class="' + item.icon + '"></i>' +
                    '<span class="nav-label">' + item.text + '</span>' +
                    '</a>' +
                    '</li>';
            }
            menuAry.push(menu);
        });
        $('#userName').html(dta.username);
        $('#side-menu').append(menuAry.join(''));
    };
    if (!dta) {
        request.send(function (obj) {
            if (obj.code == '0') {
                dta = obj.data;
                var objStr=JSON.stringify(dta);
                $global.fn.storeUserData(objStr);
                loadGlobalMenu();
            }
        });
    } else {
        loadGlobalMenu();
    }
    $("#side-menu").metisMenu(), $(".right-sidebar-toggle").click(function () {
        $("#right-sidebar").toggleClass("sidebar-open")
    });
    contabs.init();
    /**
     * 安全退出
     */
    $('.loginOut-btn').click(function () {
        layer.confirm('确定退出？', {
            btn: ['确定', '取消'], //按钮
            skin: 'layui-layer-molv'
        }, function () {
            $http.post($setting.config.index.logout).send(function (obj) {
                if (obj.code != '0') {
                    layer.msg(obj.message);
                } else {
                    window.location.href = $setting.getLocalUri() + '/login.html';
                }
            });
        });
    });
    exports('../app/index', {});

    $('#editPassword').on('click', function () {
        $editPassModal.modal('show');
    });


    $('#editpass-submit-btn').click(function () {
        var oldpassword=$oldpassword.val();
        var newpassword=$newpassword.val();
        var newpassAgain=$newpassAgain.val();

        if(oldpassword=="" || newpassword==""|| newpassAgain=="") {
            layer.msg("密码不能为空");
            return;
        }

        if(newpassword!=newpassAgain){
            layer.msg("两次密码输入不一致");
            return;
        }

        if(newpassword==oldpassword){
            layer.msg("新密码不能和旧密码一样");
            return;
        }

        $http.post($setting.config.user.editpass, {
            newpassword: newpassword,
            oldpassword:oldpassword
        }).send(function (obj) {
            if (obj.code == '0') {
                $editPassModal.modal('hide');
                layer.msg("修改密码成功");
            }
            else {
                layer.msg(obj.message);
            }
        });
    });

});